package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.function.LongFunction;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping(path= "/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping
    public Result findpage(QueryPageBean queryPageBean){
        //分页查询权限信息
        log.info("进入了分页查询");
        try {
           PageResult pageResult = this.permissionService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @PostMapping
    public Result handleAdd(@RequestBody Permission permission){
        try {
            log.info("进入了添加权限");
            this.permissionService.add(permission);
            return new Result(true,MessageConstant.ADD_PERMISSION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 编辑权限根据id进行回显
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result handleUpdate(@PathVariable("id") Integer id){
        log.info("进入了编辑权限方法");
        try {
            Permission permission =  this.permissionService.selectById(id);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS,permission);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    /**
     * 确认修改数据
     */
    @PutMapping
    public Result handleEdit(@RequestBody Permission permission){
        log.info("进入确认修改程序");
        try {
            this.permissionService.handleEdit(permission);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    /**
     * 删除权限
     * @return
     */
    @DeleteMapping(path = "/{id}")
    public Result handleDelete(@PathVariable Integer id){
        log.info("进入了删除权限方法");
        try {
            this.permissionService.handleDeleteById(id);
            return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    @GetMapping("/all")
    public Result finAllPermission(){
        log.info("进入查询所有权限方法");
        try {
            List<Permission> allPermission = permissionService.selectAllPermisson();
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,allPermission);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    /**
     * 根据MenuId查询相关permission
     * @param
     * @return
     */
    @GetMapping("/menu/association")
    public Result  findAssociationByMenuId(@PathParam("menuIds") Integer[] menuIds){
        log.info("进入根据MenuId查询相关permission方法");
        try {
            //调用Service
            List<Permission> permissionList = permissionService.findAllPermission(menuIds);
            log.info("permissionList=={}",permissionList);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }




}
